<?php 

class Geodata extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
	function load_locations($locations)
	{
		//The trick here is the $locations can be string with comma separated values, or 
		// a single string or numeric values or a list.
		
		$criteria = '0'; //This will be a comma separated list. We are adding a zero to make comma
						 // list easier to make. This can be fixed later.
		
		if(is_array($locations))
		{
			foreach($locations as $loc)
			{
				if(is_valid_location_index($loc))
				{
					$criteria .= ',' . (string)$loc;
				}
				else
				{
					var_dump($locations);
					show_error('Location index array contains something that is not a location index.');
				}
			}
		}
		else if(empty($locations))
		{
			return array();
		}
		else if(is_valid_location_index($locations) && (!(strpos($locations,","))))
		{
			$criteria = (string)(intval($locations));
		}
		else if(is_string($locations))
		{
			$locations_string_array = explode(',', $locations);
			foreach($locations_string_array as $loc_str)
			{
				if(!is_valid_location_index($loc_str))
				{
					var_dump($locations);
					show_error('Location index is not a numeric value, and it must be a numeric value');
				}
				$criteria .= ',' . (string) intval($loc_str);
			}
		}
		else
		{
			var_dump($locations);
			show_error('Location index is not something weird.');
		}

		$query = $this->db->from('geo_location')
				->where('geonameid in ('.$criteria.')')
				->get();
				
        return $query->result();		
	}
        
        //Criteria examples:
        // 'New Y' - returns everything that starts with New Y
        // 'New Y, GB' - return all New Y starting items from UK
        // 'New Y, NY, US' - in some country examples, you can also specify a state
        // 'country:GB' - return the UK
        function get_geostamps($criteria, $limit = 20, $offset = 0)
        {
            if(strtolower(substr($criteria, 0, 8)) == "country:")
            {
                $res = $this->db->like('name', trim(substr($criteria, 8)))
                            ->where('feature_class', 'A')
                            ->where('feature_code', 'PCLI')
                            ->order_by("name", "asc")
                            ->get('geo_location', $limit, $offset)->result();
                return $res;               
            }
            
            $pieces = explode(",", $criteria);
            $n = count($pieces);
            if($n > 1)
            {
                $this->db->where('country_code', trim($pieces[$n-1]));
                $criteria = $pieces[0];
            }
            $res = $this->db->like('name', $criteria, 'after' )
                                    ->order_by("name", "asc")
                                    ->get('geo_location', $limit, $offset)->result();
            return $res;                
        }        
        function get_top_geostamps($count, $offset)
        {
            $res = $this->db->order_by("name", "asc")
                            ->get('geo_location', $count, $offset)->result();
            return $res;           
        }
        function show($data, $show_also_feature_type = false)
        {
            $s = $data->name;
            if($data->country_code)
            {
                $s .= ', ' . $data->country_code;
            }
            if($show_also_feature_type)
            {
                $res = $this->db->where("geo_code_class", $data->feature_class)
                                ->where("geo_code_feature", $data->feature_code)
                                ->get('geo_features')->result();
                if($res)
                {
                    $s .= ' ('.$res[0]->name.')';
                }
            }
            return $s;
        }
}